from django.utils import timezone
from django.db import models


class ProductManager(models.Manager):
    @property
    def base_list(self):
        return self.filter(status=True)

    def get_channel_products(self, channel, status=None):
        """
        获取渠道产品
        :param channel:
        :param status:  状态
        :return:
        """
        if status is not None:
            return self.base_list.filter(channelproduct__channel=channel, channelproduct__status=True)
        else:
            return self.base_list.filter(channelproduct__channel=channel)

    def difference_products(self, channel):
        """
        获取产品差集
        :param channel:
        :return:
        """
        base_list = self.base_list
        set_qs = base_list.filter(channelproduct__channel=channel, channelproduct__status=True)
        return base_list.exclude(id__in=[i.id for i in set_qs])


class Product(models.Model):
    PRICE_TYPE = ((1, "固定金额"), (2, "百分比"),)
    TYPE = ((1, "信用卡"), (2, "贷款"), (3, "理财"), (4, "福利"), (5, "实物"),)
    JUMP_STATUS = ((1, "直接跳转,不给佣金"), (2, "录入信息"), (3, "录入车抵贷信息"), (4, "跳转前端页面"))
    SIGN_TYPE = ((1, "数据匹配"), (2, "API"), (3, "code"), (4, "主产品，不需要标记"), (5, "直接跳转不需要标记"))
    API_TYPE = ((1, "其他"), (2, "思兮"))
    is_firm = models.BooleanField(verbose_name="是否是企业才可以注册的", default=False, db_index=True)
    list_show = models.BooleanField(verbose_name="是否显示到列表上", default=False, db_index=True,
                                    help_text="子产品的不显示到列表上")
    sign_type = models.IntegerField(verbose_name="标识类型", choices=SIGN_TYPE, default=1, db_index=True)
    api_type = models.IntegerField(verbose_name="API类型", choices=API_TYPE, default=1)
    jump_status = models.IntegerField(verbose_name="跳转方式", default=2, choices=JUMP_STATUS)
    type = models.IntegerField(choices=TYPE, default=1, verbose_name="类型")
    name = models.CharField(verbose_name="名称", max_length=32)
    img = models.FileField(verbose_name="列表图", upload_to="product/list/", blank=True, null=True)
    index_logo = models.FileField(verbose_name="index_logo", upload_to="product/logo/", blank=True, null=True)
    list_logo = models.FileField(verbose_name="list_logo", upload_to="product/list_logo/", blank=True, null=True)
    banner = models.ImageField(upload_to="product/banner/", blank=True, null=True)
    special = models.CharField(verbose_name="特点(副本题)", max_length=32, null=True, blank=True)
    adv = models.TextField(verbose_name="优势", help_text="请用';'隔开")
    order = models.IntegerField(default=1, verbose_name="排序")
    url = models.CharField(max_length=800, verbose_name="产品链接", blank=True, null=True)
    one_product = models.ImageField(verbose_name="单产品分享背景图", upload_to="product/one/", blank=True, null=True)
    is_pay = models.BooleanField(verbose_name="是否给佣金", default=True)
    status = models.BooleanField(default=True, verbose_name="上架")
    sign = models.CharField(unique=True, verbose_name="标识", null=True, max_length=64)
    active_desc = models.TextField(verbose_name="优惠活动，用';'隔开，三个", blank=True, null=True)
    create_time = models.DateTimeField(verbose_name="创建时间", default=timezone.now)
    parent_product = models.ForeignKey("self", verbose_name="产品组", blank=True, null=True)
    price_type = models.IntegerField(choices=PRICE_TYPE, verbose_name="价格类型", default=1)
    objects = ProductManager()

    class Meta:
        verbose_name = "产品"  # admin后台显示
        verbose_name_plural = verbose_name  # 复数显示
        unique_together = ['type', 'order']
        ordering = ["order"]

    def __unicode__(self):
        return self.name

    def __str__(self):
        return self.name


class ProductExtend(models.Model):
    desc_img = models.ImageField(upload_to="finance/product/desc", verbose_name="详情页图")
    price = models.DecimalField(verbose_name="产品价格", max_digits=7, decimal_places=2)
    base_product = models.OneToOneField("Product", verbose_name="产品")

    def __str__(self):
        return self.base_product.name


class ProductCityRelationManager(models.Manager):
    pass


class ProductCityRelation(models.Model):
    product = models.ForeignKey("Product", verbose_name="产品")
    province = models.ForeignKey("tools.Province", verbose_name="省份")
    city = models.ForeignKey("tools.City", verbose_name="城市")
    objects = ProductCityRelationManager()

    class Meta:
        verbose_name = "产品城市关系表"  # admin后台显示
        verbose_name_plural = verbose_name  # 复数显示


class ProductApiProduct(models.Model):
    product = models.OneToOneField("Product", verbose_name="产品")
    company = models.CharField(verbose_name="公司", blank=True, null=True, max_length=16)
    api_product = models.CharField(verbose_name="API公司产品", db_index=True, max_length=8)

    class Meta:
        verbose_name = "对应API公司的产品"
        verbose_name_plural = verbose_name


